package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * ProxyGetRequestRecord类
 * 实现代理获取请求记录的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class ProxyGetRequestRecord implements AxdrType {

    private byte[] dataCode = null;
    private PIID piid = null;
    private Unsigned16 waitTime = null;
    private TSA tsa = null;
    private OAD oad = null;
    private RSD rsd = null;
    private RCSD rcsd = null;

    public ProxyGetRequestRecord() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(PIID piid, Unsigned16 waitTime, TSA tsa, OAD oad, RSD rsd, RCSD rcsd) {
        this.piid = piid;
        this.waitTime = waitTime;
        this.tsa = tsa;
        this.oad = oad;
        this.rsd = rsd;
        this.rcsd = rcsd;
    }

    public void setPIID(PIID piid) {
        this.piid = piid;
    }

    public void setWaitTime(Unsigned16 waitTime) {
        this.waitTime = waitTime;
    }

    public void setTsa(TSA tsa) {
        this.tsa = tsa;
    }

    public void setOAD(OAD oad) {
        this.oad = oad;
    }

    public void setRSD(RSD rsd) {
        this.rsd = rsd;
    }

    public void setRCSD(RCSD rcsd) {
        this.rcsd = rcsd;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.piid = new PIID();
        this.piid.setNumBit(8);
        codeLength += this.piid.decode(input);

        this.waitTime = new Unsigned16();
        this.waitTime.setConst();
        codeLength += this.waitTime.decode(input);

        this.tsa = new TSA();
        codeLength += this.tsa.decode(input);

        this.oad = new OAD();
        codeLength += this.oad.decode(input);

        this.rsd = new RSD();
        codeLength += this.rsd.decode(input);

        this.rcsd = new RCSD();
        codeLength += this.rcsd.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.rcsd != null && this.rsd != null && this.oad != null && this.tsa != null && this.waitTime != null
                    && this.piid != null) {
                codeLength = 0;
                codeLength += this.rcsd.encode(output);
                codeLength += this.rsd.encode(output);
                codeLength += this.oad.encode(output);
                codeLength += this.tsa.encode(output);
                codeLength += this.waitTime.encode(output);
                codeLength += this.piid.encode(output);
            } else {
                throw new RuntimeException("rcsd || rsd || oad || tsa || waittime || piid is null");
            }
        }

        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            this.encode(revOStream);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error encoding ProxyGetRequestRecord", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.piid != null && this.waitTime != null && this.tsa != null && this.oad != null && this.rsd != null
                && this.rcsd != null) {
            return "sequence: {" + "piid: " + this.piid + ", waittime: " + this.waitTime + ", tas: " + this.tsa
                    + ", oad: " + this.oad + ", rsd: " + this.rsd + ", rcsd: " + this.rcsd + "}";
        } else {
            return "rcsd || rsd || oad || tsa || waittime || piid is null";
        }
    }

    public PIID getPiid() {
        return piid;
    }

    public Unsigned16 getWaitTime() {
        return waitTime;
    }

    public TSA getTsa() {
        return tsa;
    }

    public OAD getOad() {
        return oad;
    }

    public RSD getRsd() {
        return rsd;
    }

    public RCSD getRcsd() {
        return rcsd;
    }
}